Micron Document
`:top
An `F33f`_`[SQL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=SQL]`_`f `!UPDATE`! statement changes the data of one or more records in a `F33f`_`[table`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Table_(database)]`_`f. Either all the rows can be updated, or a subset may be chosen using a `F33f`_`[condition`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Condition_(SQL)]`_`f.

The `B100`F9d9UPDATE`f`b statement has the following form:`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f]

`!`B100`F9d9UPDATE`f`b`! `*table_name`* `!`B100`F9d9SET`f`b`! `*column_name`* = `*value`* [, `*column_name`* = `*value ...`*] [`!`B100`F9d9WHERE`f`b`! `*condition`*]

For the `B100`F9d9UPDATE`f`b to be successful, the user must have data manipulation privileges (`B100`F9d9UPDATE`f`b privilege) on the table or `F33f`_`[column`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Column_(database)]`_`f and the updated value must not conflict with all the applicable constraints (such as `F33f`_`[primary keys`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Primary_key]`_`f, unique indexes, `F33f`_`[CHECK constraints`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Check_Constraint]`_`f, and `F33f`_`[NOT NULL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Null_(SQL)]`_`f constraints).

In some databases, such as `F33f`_`[PostgreSQL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=PostgreSQL]`_`f, when a `F33f`_`[FROM clause`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=From_(SQL)]`_`f is present, what essentially happens is that the target table is joined to the tables mentioned in the fromlist, and each output row of the join represents an update operation for the target table. When using FROM, one should ensure that the join produces at most one output row for each row to be modified. In other words, a target row shouldn't join to more than one row from the other table(s). If it does, then only one of the join rows will be used to update the target row, but which one will be used is not readily predictable.`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f]

Because of this indeterminacy, referencing other tables only within sub-selects is safer, though often harder to read and slower than using a join.

MySQL does not conform to ANSI standard.`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f]

>>Contents

• `F0af`_`[Examples`#examples]`_`f
• `F0af`_`[Potential issues`#potential-issues]`_`f
• `F0af`_`[References`#references]`_`f

-─

>>Examples

Set the value of column `*C1`* in table `*T`* to 1, only in those rows where the value of column `*C2`* is "a".

`B100`F9d9UPDATE T`f`b
`B100`F9d9 SET C1 = 1`f`b
`B100`F9d9 WHERE C2 = 'a'`f`b

In table `*T`*, set the value of column `*C1`* to 9 and the value of `*C3`* to 4 for all rows for which the value of column `*C2`* is "a".

`B100`F9d9UPDATE T`f`b
`B100`F9d9 SET C1 = 9,`f`b
`B100`F9d9 C3 = 4`f`b
`B100`F9d9 WHERE C2 = 'a'`f`b

Increase value of column `*C1`* by 1 if the value in column `*C2`* is "a".

`B100`F9d9UPDATE T`f`b
`B100`F9d9 SET C1 = C1 + 1`f`b
`B100`F9d9 WHERE C2 = 'a'`f`b

Prepend the value in column `*C1`* with the string "text" if the value in column `*C2`* is "a".

`B100`F9d9UPDATE T`f`b
`B100`F9d9 SET C1 = 'text' || C1`f`b
`B100`F9d9 WHERE C2 = 'a'`f`b

Set the value of column `*C1`* in table `*T1`* to 2, only if the value of column `*C2`* is found in the sublist of values in column `*C3`* in table `*T2`* having the column `*C4`* equal to 0.

`B100`F9d9UPDATE T1`f`b
`B100`F9d9 SET C1 = 2`f`b
`B100`F9d9 WHERE C2 IN ( SELECT C3`f`b
`B100`F9d9 FROM T2`f`b
`B100`F9d9 WHERE C4 = 0)`f`b

One may also update multiple columns in a single update statement:

`B100`F9d9UPDATE T`f`b
`B100`F9d9 SET C1 = 1,`f`b
`B100`F9d9 C2 = 2`f`b

Complex conditions and JOINs are also possible:

`B100`F9d9UPDATE T`f`b
`B100`F9d9 SET A = 1`f`b
`B100`F9d9 WHERE C1 = 1`f`b
`B100`F9d9 AND C2 = 2`f`b

Some databases allow the non-standard use of the FROM clause:

`B100`F9d9UPDATE a`f`b
`B100`F9d9 SET a.[updated_column] = updatevalue`f`b
`B100`F9d9 FROM articles a`f`b
`B100`F9d9 JOIN classification c`f`b
`B100`F9d9 ON a.articleID = c.articleID`f`b
`B100`F9d9 WHERE c.classID = 1`f`b

Or on Oracle systems (assuming there is an index on classification.articleID):

`B100`F9d9UPDATE`f`b
`B100`F9d9(`f`b
`B100`F9d9 SELECT *`f`b
`B100`F9d9 FROM articles`f`b
`B100`F9d9 JOIN classification`f`b
`B100`F9d9 ON articles.articleID = classification.articleID`f`b
`B100`F9d9 WHERE classification.classID = 1`f`b
`B100`F9d9)`f`b
`B100`F9d9SET [updated_column] = updatevalue`f`b

With long name of table:

`B100`F9d9UPDATE MyMainTable AS a`f`b
`B100`F9d9SET a.LName = Smith`f`b
`B100`F9d9WHERE a.PeopleID = 1235`f`b

>>Potential issues

• See `F33f`_`[Halloween Problem`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Halloween_Problem]`_`f. It is possible for certain kinds of `B100`F9d9UPDATE`f`b statements to become an `F33f`_`[infinite loop`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Infinite_loop]`_`f when the `B100`F9d9WHERE`f`b clause and one or more `B100`F9d9SET`f`b clauses may utilize an intertwined `F33f`_`[index`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Index_(database)]`_`f.

>>References

`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f http://dev.mysql.com/doc/refman/5.0/en/update.html simplified from this page
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f "UPDATE". January 2012.
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f "SQL - Update a table column, then the other column with updated value of the former. MySQL / PostgreSQL differ".

`c`F0af`_`[↑ Back to top`#top]`_`f`a